package com.pyue.eam.job.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pyue.eam.admin.api.entity.regi.CqCompanyEntity;
import com.pyue.eam.admin.api.entity.regi.CqRegiLibEntity;
import com.pyue.eam.app.mapper.AppUserMapper;
import com.pyue.eam.regi.service.CqCompanyService;
import com.pyue.eam.regi.service.CqRegiLibService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class RegiCompanyImpl {
    private final CqRegiLibService cqRegiLibService;
    private final CqCompanyService cqCompanyService;
    private final AppUserMapper appUserMapper;

    /**
     * 配置公司和团长头像空白的设为负责人头像
     */
    public void companyAndRegiHeadJob() {

        // 团长头像
        List<CqRegiLibEntity> regiList = cqRegiLibService.list(Wrappers.lambdaQuery(CqRegiLibEntity.class).isNull(CqRegiLibEntity::getRegiAvatar));
        if (CollUtil.isNotEmpty(regiList)) {
            List<CqRegiLibEntity> newRegiList = regiList.stream()
                    .filter(e -> e.getCreateBy() != null)
                    .peek(e -> e.setRegiAvatar(appUserMapper.getAvatarByUserId(e.getCreateBy())))
                    .collect(Collectors.toList());
            if (CollUtil.isNotEmpty(newRegiList)) {
                cqRegiLibService.updateBatchById(newRegiList);
            }
        }

        // 公司头像
        List<CqCompanyEntity> companyEntityList = cqCompanyService.list(Wrappers.lambdaQuery(CqCompanyEntity.class).isNull(CqCompanyEntity::getCompanyAvatar));
        if (CollUtil.isNotEmpty(companyEntityList)) {
            companyEntityList.stream()
                    .filter(e -> e.getCreateBy() != null)
                    .forEach(e -> {
                        e.setCompanyAvatar(appUserMapper.getAvatarByUserId(e.getCreateBy()));
                        cqCompanyService.updateById(e);
                    });
        }

    }
}
